package viethp.mtrade.helpers;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import viethp.mtrade.activities.R;
import viethp.mtrade.common.GlobalVariables;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.Toast;

public class Common_Helper {

	// Call photo gallery
	public static void callGallery(Activity activity, int requestCode) {
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		activity.startActivityForResult(
				Intent.createChooser(intent, "Select Picture"), requestCode);
	}

	// Show Toast
	public static void showToast(Context context, String str_message) {
		Toast toast = Toast.makeText(context, str_message, Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 0);
		toast.setDuration(GlobalVariables.TIME_WAIT);
		toast.show();
	}

	// Show warning alert
	public static void showWarning(Context context, String str_message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("PassOnSale").setMessage(str_message)
				.setIcon(R.drawable.ic_dialog_alert).setCancelable(false)
				.setPositiveButton("OK", null);
		builder.create().show();
	}

	// Resize imgae
	public static Bitmap bitmapResize(Bitmap bitmap, int newWidth, int newHeight) {
		if (bitmap == null)
			return bitmap;
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		// calculate the scale - in this case = 0.4f
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;

		// createa matrix for the manipulation
		Matrix matrix = new Matrix();
		// resize the bit map
		matrix.postScale(scaleWidth, scaleHeight);

		// recreate the new Bitmap
		Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,
				matrix, true);
		return resizedBitmap;
	}

	// Share Email
	public static void shareByEmail(Context context, String emailAddress) {
		final Intent emailIntent = new Intent(
				android.content.Intent.ACTION_SEND);
		emailIntent.setType("text/plain");
		emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
				"Check out the PassOnSale App");
		String content = "PassOnSale";
		emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, content);

		emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { emailAddress });
		try {
			context.startActivity(Intent.createChooser(emailIntent,
					"Complete action using"));
		} catch (android.content.ActivityNotFoundException ex) {
			Toast.makeText(null, "There are no email clients installed.",
					Toast.LENGTH_SHORT).show();
		}
	}

	// Check email address validation
	public static boolean eMailValidation(String emailstring) {
		Pattern emailPattern = Pattern.compile(".+@.+\\.[a-z]+");
		Matcher emailMatcher = emailPattern.matcher(emailstring);
		return emailMatcher.matches();
	}

	// Set reload county list
/*	public static void setIsLoadCountyList(boolean isLoad, Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				GlobalVariables.PREFERENCES, 0);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putBoolean("LOAD_COUNTY", isLoad);
		editor.commit();
	}

	public static boolean getIsLoadCountyList(Context context) {
		int mode = Activity.MODE_PRIVATE;
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				GlobalVariables.PREFERENCES, mode);
		return sharedPreferences.getBoolean("LOAD_COUNTY", false);
	}*/

	public static String toMD5(String s) {
		try {
			// Create MD5 Hash
			MessageDigest digest = java.security.MessageDigest
					.getInstance("MD5");
			digest.update(s.getBytes());
			byte messageDigest[] = digest.digest();

			// Create Hex String
			StringBuffer hexString = new StringBuffer();
			for (int i = 0; i < messageDigest.length; i++) {
				String hex = Integer.toHexString(0xFF & messageDigest[i]);
				if (hex.length() == 1)
					hexString.append('0');
				hexString.append(hex);
			}
			return hexString.toString().toUpperCase();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return "";

	}

	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

/*	public static User getUserLogin(Context context) {
		User user = new User();
		int mode = Activity.MODE_PRIVATE;
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				GlobalVariables.PREFERENCES, mode);
		user.setId(sharedPreferences.getInt("USER_ID", -1));

		return user;
	}

	public static void setUserLogin(User user, Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				GlobalVariables.PREFERENCES, 0);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putInt("USER_ID", user.getId());

	}
*/
	public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

		int width = bm.getWidth();

		int height = bm.getHeight();

		float scaleWidth = ((float) newWidth) / width;

		float scaleHeight = ((float) newHeight) / height;

		// create a matrix for the manipulation

		Matrix matrix = new Matrix();

		// resize the bit map

		matrix.postScale(scaleWidth, scaleHeight);

		// recreate the new Bitmap

		Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
				matrix, false);

		return resizedBitmap;

	}

/*	public static String checkcomment(int n) {
		if (n > 1)
			return " Comments";
		return " Comment";

	}*/

	// Set login via Facebook
/*	public static void setIsFBLogin(boolean isAdd, Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				GlobalVariables.PREFERENCES, 0);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		editor.putBoolean("LOGIN_FACEBOOK", isAdd);
		editor.commit();
	}

	// Get login via Facebook
	public static boolean getIsFBLogin(Context context) {
		int mode = Activity.MODE_PRIVATE;
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				GlobalVariables.PREFERENCES, mode);
		return sharedPreferences.getBoolean("LOGIN_FACEBOOK", false);
	}*/
	
public static	String ReplaceSpacing(String st)
	{	st=st.trim();
		String temp="";
		for(int i=0;i<st.length();i++)
		{
			if(st.charAt(i)==' ') temp+="_";
			else temp+=st.charAt(i);
		}
		return temp;
	}
public static String now()
{     Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(cal.getTime());
}   

}
